﻿@using System.Diagnostics;
@using System.Collections;
@using System.Reflection;
@using System.Runtime;
@using System.Runtime.Versioning;
@using Microsoft.AspNetCore.Hosting
@using Microsoft.AspNetCore.Http.Extensions
@using NewLife
@using NewLife.Caching
@using NewLife.Common;
@using NewLife.Cube
@using NewLife.Reflection
@using NewLife.Web
@using XCode.Membership
@inject Microsoft.AspNetCore.Http.IHttpContextAccessor HttpContextAccessor
@inject IWebHostEnvironment Env
@{
    //Layout = NewLife.Cube.Setting.Current.Layout;

    ViewBag.Title = "服务器信息";

    var asm = Assembly.GetExecutingAssembly();
    var att = asm.GetCustomAttribute<TargetFrameworkAttribute>();
    var ver = att?.FrameworkDisplayName ?? att?.FrameworkName;
    var httpContext = HttpContextAccessor.HttpContext;
    var req = httpContext.Request;
    var conn = httpContext.Connection;

    var mi = MachineInfo.Current ?? new MachineInfo();

    // GC设置
    var gc = $"IsServerGC={GCSettings.IsServerGC},LatencyMode={GCSettings.LatencyMode}";
}

@section css{
    <style>
        /*服务器信息*/
        #ServerInfo .laytable-cell-1-0-0 {
            width: 100px;
        }

        #ServerInfo .laytable-cell-1-0-1 {
        }

        #ServerInfo .laytable-cell-1-0-2 {
            width: 100px;
        }

        #ServerInfo .laytable-cell-1-0-3 {
        }

        /*程序集信息*/
        #AssemblyInfo .laytable-cell-1-0-0 {
        }

        #AssemblyInfo .laytable-cell-1-0-1 {
        }

        #AssemblyInfo .laytable-cell-1-0-2 {
        }

        #AssemblyInfo .laytable-cell-1-0-3 {
        }

        #AssemblyInfo .laytable-cell-1-0-4 {
        }

        #AssemblyInfo .laytable-cell-1-0-5 {
            width: 700px;
        }

        #mainpage a:focus, a:hover {
            color: #23527c;
        }

        #mainpage a {
            color: #337ab7;
        }
    </style>
}

<div class="layui-fluid" id="mainpage">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">
                    服务器信息
                </div>
                <div class="layui-card-body">
                    <div class="layui-form layui-border-box layui-table-view">
                        <div class="layui-table-box" id="ServerInfo">
                            <div class="layui-table-header">
                                <table class="layui-table" cellspacing="0" cellpadding="0" border="0" lay-size="sm" style="width:100%">
                                    <thead>
                                        <tr>
                                            <th colspan="4">
                                                <div class="layui-table-cell laytable-cell-group">
                                                    <span>
                                                        服务器信息
                                                    </span>
                                                </div>
                                            </th>
                                        </tr>
                                    </thead>
                                </table>
                            </div>
                            <div class="layui-table-body">
                                <table class="layui-table" cellspacing="0" cellpadding="0" border="0" lay-size="sm" style="width:100%">
                                    <tbody>
                                        <tr>
                                            <td>
                                                <div class="layui-table-cell laytable-cell-1-0-0" data-id="1-0-0">
                                                    应用系统：
                                                </div>
                                            </td>
                                            <td>
                                                <div class="layui-table-cell laytable-cell-1-0-1" data-id="1-0-1">
                                                    &nbsp;
                                                    @if (this.Has((PermissionFlags)16))
                                                    {
                                                        <a style="cursor: pointer;" data-action="action" data-href="@Url.Action("Restart")" data-confirm="仅重启ASP.Net Core应用程序域，而不是操作系统！<br/>确认重启？">重启应用系统</a>
                                                    }
                                                    &nbsp;&nbsp;&nbsp;&nbsp;@req.GetRawUrl()
                                                </div>
                                            </td>
                                            <td>
                                                <div class="layui-table-cell laytable-cell-1-0-2" data-id="1-0-2">
                                                    目录：
                                                </div>
                                            </td>
                                            <td>
                                                <div class="layui-table-cell laytable-cell-1-0-3" data-id="1-0-3">
                                                    @Env.ContentRootPath
                                                </div>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <div class="layui-table-cell laytable-cell-1-0-0">
                                                    域名地址：
                                                </div>
                                            </td>
                                            <td>
                                                <div class="layui-table-cell laytable-cell-1-0-1">
                                                    @{
                                                        var addrLocal = conn.LocalIpAddress;
                                                        var addrRemote = conn.RemoteIpAddress;
                                                        if (addrLocal != null && addrLocal.IsIPv4MappedToIPv6) addrLocal = addrLocal.MapToIPv4();
                                                        if (addrRemote != null && addrRemote.IsIPv4MappedToIPv6) addrRemote = addrRemote.MapToIPv4();
                                                        var userHost = httpContext.GetUserHost();
                                                    }
                                                    <span title="主机">@req.Headers["Host"]</span>，
                                                    <span title="本地">@addrLocal:@conn.LocalPort</span>
                                                    &nbsp;<span title="远程">[@addrRemote:@conn.RemotePort]</span>
                                                    @if (addrRemote + "" != userHost)
                                                    {
                                                        <span title="真实">&nbsp;[@httpContext.GetUserHost()]</span>
                                                    }
                                                </div>
                                            </td>
                                            <td>
                                                <div class="layui-table-cell laytable-cell-1-0-2">
                                                    应用程序：
                                                </div>
                                            </td>
                                            <td>
                                                <div class="layui-table-cell laytable-cell-1-0-3">
                                                    <span title="@Environment.CommandLine">@Process.GetCurrentProcess().ProcessName</span>
                                                </div>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <div class="layui-table-cell laytable-cell-1-0-0">
                                                    应用域：
                                                </div>
                                            </td>
                                            <td>
                                                <div class="layui-table-cell laytable-cell-1-0-1">
                                                    @AppDomain.CurrentDomain.FriendlyName
                                                    <a href="@Url.Action("Main", new{ id = "Assembly" })" target="_blank" title="点击打开进程程序集列表">程序集列表</a>
                                                    <a href="@Url.Action("Main", new{ id = "ProcessModules" })" target="_blank" title="点击打开进程模块列表">模块列表</a>
                                                    <a href="@Url.Action("Main", new{ id = "ServerVar" })" target="_blank" title="点击打开服务器变量列表">服务器变量列表</a>
                                                </div>
                                            </td>
                                            <td>
                                                <div class="layui-table-cell laytable-cell-1-0-2">
                                                    .Net 版本：
                                                </div>
                                            </td>
                                            <td>
                                                <div class="layui-table-cell laytable-cell-1-0-3">
                                                    @Environment.Version &nbsp;@ver
                                                </div>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td title="@mi.Guid">
                                                <div class="layui-table-cell laytable-cell-1-0-0">
                                                    操作系统：
                                                </div>
                                            </td>
                                            <td>
                                                <div class="layui-table-cell laytable-cell-1-0-1">
                                                    @mi.OSName @mi.OSVersion
                                                </div>
                                            </td>
                                            <td title="@mi.UUID">
                                                <div class="layui-table-cell laytable-cell-1-0-2">
                                                    机器用户：
                                                </div>
                                            </td>
                                            <td>
                                                <div class="layui-table-cell laytable-cell-1-0-3">
                                                    @if (!mi.Product.IsNullOrEmpty())
                                                    {
                                                        <apan>@mi.Product，</apan>
                                                    }
                                                    @Environment.UserName/@Environment.MachineName
                                                </div>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <div class="layui-table-cell laytable-cell-1-0-0">
                                                    处理器：
                                                </div>
                                            </td>
                                            <td>
                                                <div class="layui-table-cell laytable-cell-1-0-1">
                                                    @mi.Processor，
                                                    @Environment.ProcessorCount
                                                    核心，@mi.CpuRate.ToString("p0")
                                                    @if (mi.Temperature > 0)
                                                    {
                                                        <span>，@mi.Temperature ℃</span>
                                                    }
                                                </div>
                                            </td>
                                            <td>
                                                <div class="layui-table-cell laytable-cell-1-0-2">
                                                    时间：
                                                </div>
                                            </td>
                                            <td>
                                                @{
                                                    var uptime = TimeSpan.FromMilliseconds(Environment.TickCount64);
                                                }
                                                <div class="layui-table-cell laytable-cell-1-0-3">
                                                    @DateTimeOffset.Now，开机@(uptime.ToString(@"dd\.hh\:mm\:ss"))
                                                </div>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                @{ var process = Process.GetCurrentProcess();}
                                                <div class="layui-table-cell laytable-cell-1-0-0">
                                                    内存：
                                                </div>
                                            </td>
                                            <td>
                                                <div class="layui-table-cell laytable-cell-1-0-1">
                                                    物理：@((mi.AvailableMemory / 1024 / 1024).ToString("n0"))M / @((mi.Memory / 1024 / 1024).ToString("n0"))M，
                                                    工作/提交: @((process.WorkingSet64 / 1024 / 1024).ToString("n0"))M/@((process.PrivateMemorySize64 / 1024 / 1024).ToString("n0"))M
                                                    GC: @((GC.GetTotalMemory(false) / 1024 / 1024).ToString("n0"))M
                                                    <a href="@Url.Action("MemoryFree")" title="点击释放进程内存">释放内存</a>
                                                </div>
                                            </td>
                                            <td>
                                                <div class="layui-table-cell laytable-cell-1-0-2">
                                                    进程时间：
                                                </div>
                                            </td>
                                            <td>
                                                <div class="layui-table-cell laytable-cell-1-0-3">
                                                    @process.TotalProcessorTime.TotalSeconds.ToString("N2")秒 启动于 @process.StartTime.ToLocalTime().ToFullString()
                                                </div>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <div class="layui-table-cell laytable-cell-1-0-0">
                                                    Session：
                                                </div>
                                            </td>
                                            <td>
                                                <div class="layui-table-cell laytable-cell-1-0-1">
                                                    @httpContext.Session.Keys.Count() 个
                                                    <a href="@Url.Action("Main", new{ id = "Session" })" target="_blank" title="点击打开Session列表">Session列表</a>
                                                    ，@gc
                                                </div>
                                            </td>
                                            <td>
                                                <div class="layui-table-cell laytable-cell-1-0-2">
                                                    @{ var app = ApplicationManager.Load();}
                                                    应用启动：
                                                </div>
                                            </td>
                                            <td>
                                                <div class="layui-table-cell laytable-cell-1-0-3">
                                                    启动于 @app.StartTime.ToLocalTime().ToFullString()
                                                </div>
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">
                    程序集信息
                </div>
                <div class="layui-card-body">
                    <div class="layui-form layui-border-box layui-table-view">
                        <div class="layui-table-box" id="AssemblyInfo">
                            <div class="layui-table-header">
                                <table class="layui-table" cellspacing="0" cellpadding="0" border="0" lay-size="sm" style="width:100%">
                                    <thead>
                                        <tr>
                                            <th>
                                                <div class="layui-table-cell laytable-cell-1-0-0" data-id="1-0-0">
                                                    <span>
                                                        名称
                                                    </span>
                                                </div>
                                            </th>
                                            <th>
                                                <div class="layui-table-cell laytable-cell-1-0-1" data-id="1-0-1">
                                                    <span>
                                                        标题
                                                    </span>
                                                </div>
                                            </th>
                                            <th>
                                                <div class="layui-table-cell laytable-cell-1-0-2" data-id="1-0-2">
                                                    <span>
                                                        文件版本
                                                    </span>
                                                </div>
                                            </th>
                                            <th>
                                                <div class="layui-table-cell laytable-cell-1-0-3" data-id="1-0-3">
                                                    <span>
                                                        内部版本
                                                    </span>
                                                </div>
                                            </th>
                                            <th>
                                                <div class="layui-table-cell laytable-cell-1-0-4" data-id="1-0-4">
                                                    <span>
                                                        编译时间
                                                    </span>
                                                </div>
                                            </th>
                                            <th>
                                                <div class="layui-table-cell laytable-cell-1-0-5" data-id="1-0-5">
                                                    <span>
                                                        描述
                                                    </span>
                                                </div>
                                            </th>
                                        </tr>
                                    </thead>
                                </table>
                            </div>
                            <div class="layui-table-body layui-table-main">
                                <table class="layui-table" cellspacing="0" cellpadding="0" border="0" lay-size="sm" style="width:100%">
                                    <tbody>
                                        @foreach (AssemblyX item in ViewBag.MyAsms)
                                        {
                                            <tr>
                                                <td>
                                                    <div class="layui-table-cell laytable-cell-1-0-0" data-id="1-0-0">
                                                        @item.Name
                                                    </div>
                                                </td>
                                                <td>
                                                    <div class="layui-table-cell laytable-cell-1-0-1" data-id="1-0-1">
                                                        @item.Title
                                                    </div>
                                                </td>
                                                <td>
                                                    <div class="layui-table-cell laytable-cell-1-0-2" data-id="1-0-2">
                                                        @item.FileVersion
                                                    </div>
                                                </td>
                                                <td>
                                                    <div class="layui-table-cell laytable-cell-1-0-3" data-id="1-0-3">
                                                        @item.Version
                                                    </div>
                                                </td>
                                                <td title="@item.Version">
                                                    <div class="layui-table-cell laytable-cell-1-0-4" data-id="1-0-4">
                                                        @(item.Compile.Year <= 2000 ? "" : item.Compile.ToFullString())
                                                    </div>
                                                </td>
                                                <td title="@item.Description">
                                                    <div class="layui-table-cell laytable-cell-1-0-5" data-id="1-0-5">
                                                        @item.Description
                                                    </div>
                                                </td>
                                            </tr>
                                        }
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

@section scripts{
    <script>
        layui.use(['jquery'], function () {
            var $ = layui.$;

            setTableScroll();
            setTable();
        });
    </script>
}